package com.example.chapter06;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter06.database.ShoppingDBHelper;
import com.example.chapter06.entity.GoodsInfo;
import com.example.chapter06.util.ToastUtil;

import java.util.List;

public class ShoppingChannelActivity extends AppCompatActivity implements View.OnClickListener {

    //声明一个商品数据库的帮助器对象
    private ShoppingDBHelper mDBHelper;
    private TextView tv_count;
    private GridLayout glChannel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_channel);
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText("手机商场");

        tv_count = findViewById(R.id.tv_count);
        glChannel = findViewById(R.id.gl_channel);
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_cart).setOnClickListener(this);

        mDBHelper = ShoppingDBHelper.getInstance(this);
        mDBHelper.openWriteLink();
        mDBHelper.openReadLink();

        //从数据库查询出商品信息，并展示
        showGoods();

    }

    /*当界面可以与用户交互的时候，执行一次*/
    @Override
    protected void onResume() {
        super.onResume();
        //查询购物车商品总数，并展示
        showCartInfoTotal();
    }

    //查询购物车商品总数，并展示
    private void showCartInfoTotal() {
        int count = mDBHelper.countCartInfo();
        MyApplication.getInstance().goodsCount = count;
        tv_count.setText(String.valueOf(count));
    }

    private void showGoods() {
        //在Java代码中设置宽高为wrapContent才能在Java代码中设置宽高
        //商品条目是一个线性布局，设置布局的宽度为屏幕的一半
        int screenWidth = getResources().getDisplayMetrics().widthPixels;//获取屏幕的宽度
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth / 2,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //查询商品数据库中的所有商品记录
        List<GoodsInfo> list = mDBHelper.queryAllGoodsInfo();

        //移除下面的所有子视图
        glChannel.removeAllViews();

        for (GoodsInfo info : list) {
            //获取布局文件item_goods.xml的根视图
            @SuppressLint("InflateParams") View view = LayoutInflater.from(this)
                    .inflate(R.layout.item_goods, null);
            ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
            TextView tv_name = view.findViewById(R.id.tv_goods_name);
            TextView tv_price = view.findViewById(R.id.tv_price);
            Button btn_add = view.findViewById(R.id.btn_add);

            //给控件设置值
            iv_thumb.setImageURI(Uri.parse(info.getPicPath()));
            tv_name.setText(info.getName());
            tv_price.setText(String.valueOf(info.getPrice()));

            //给添加到购物车添加点击事件
            btn_add.setOnClickListener(v -> {
                addToCart(info.getId(), info.getName());
            });

            //点击商品图片，跳转到商品详情页面
            iv_thumb.setOnClickListener(v->{
                Intent intent = new Intent(this, ShoppingDetailActivity.class);
                intent.putExtra("goods_id",info.getId());
                startActivity(intent);
            });

            glChannel.addView(view, params);
        }
    }

    //把指定编号的商品添加到购物车
    private void addToCart(int goodsId, String name) {
        //购物车商品数量+1
        int count = ++MyApplication.getInstance().goodsCount;
        tv_count.setText(String.valueOf(count));
        mDBHelper.insertCartInfo(goodsId);
        ToastUtil.show(this, "已添加一部" + name + "到购物车");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDBHelper.closeLink();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.iv_back){
            //点击了返回图标，关闭当前页面
            finish();
        }
        if(v.getId() == R.id.iv_cart){

            //从商场页面跳转到购物车页面
            Intent intent = new Intent(this,ShoppingCartActivity.class);
            //设置启动标志，避免多次返回同一页面
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }
}